/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.*;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.*;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.AgriProductService;
import com.jckj.core.util.StringUtils;
import com.jckj.core.vos.AgriProductQueryVo;
import com.sun.tools.javac.comp.Check;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * </p>
 *
 * @author 刘志远
 * @date 18-3-5
 */
@Service
@Transactional(readOnly = true, rollbackFor = ServiceException.class)
public class AgriProductServiceImpl implements AgriProductService {

    private static Logger LOGGER = LoggerFactory.getLogger(OfficeServiceImpl.class);

    @Autowired
    private AgriProViewMapper agriProViewMapper;

    @Autowired
    private AgriProductMapper agriProductMapper;

    @Autowired
    private FarmParkAgriProductMapper farmParkAgriProductMapper;

    @Autowired
    private ProducttionLinkMapper producttionLinkMapper;

    @Autowired
    private CheckSegmentMapper checkSegmentMapper;

    @Override
    public PageInfo<AgriProduct> queryArgiProduct(AgriProductQueryVo vo) {
        return null;
    }

    @Override
    public void addProLink(ProducttionLink producttionLink) {

    }

    @Override
    public void updateProLink(ProducttionLink producttionLink, String uId) {

    }

    @Override
    public void deleteProLink(ProducttionLink producttionLink) {

    }

    @Override
    public List<ProducttionLink> queryAllProLink(AgriProductQueryVo vo) {
        return null;
    }

    @Override
    public void addCheckSegment(CheckSegment checkSegment) {

    }

    @Override
    public void updateSegment(CheckSegment checkSegment) {

    }

    @Override
    public void deleteSegment(CheckSegment checkSegment) {

    }

    @Override
    public List<CheckSegment> queryAllSegment(AgriProductQueryVo vo) {
        return null;
    }

    @Override
    public void addCheckSegmentItem(CheckSegmentItem checkSegmentItem) {

    }

    @Override
    public void updateCheckSegmentItem(CheckSegmentItem checkSegmentItem) {

    }

    @Override
    public void deleteCheckSegmentItem(CheckSegmentItem checkSegmentItem) {

    }

    @Override
    public List<CheckSegmentItem> queryAllCheckSegmentItem(AgriProductQueryVo agriProductQueryVo) {
        return null;
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public PageInfo<AgriProView> findAgriProViewByPage(AgriProductQueryVo agriProductQueryVo) {
        Page<AgriProView> page = PageHelper.startPage(agriProductQueryVo.getPage(), agriProductQueryVo.getLimit());
        AgriProViewExample agriProViewExample = new AgriProViewExample();
        AgriProViewExample.Criteria criteria = agriProViewExample.createCriteria();
        if(StringUtils.isNotBlank(agriProductQueryVo.getProCode())){
            criteria.andProCodeLike(agriProductQueryVo.getProCode() + "%");
        }
        if(StringUtils.isNotBlank(agriProductQueryVo.getProName())){
            criteria.andProNameLike(agriProductQueryVo.getProName() + "%");
        }
        if(StringUtils.isNotEmpty(agriProductQueryVo.getType())){
            criteria.andProTypeEqualTo(agriProductQueryVo.getType());
        }
        List<AgriProView> parks = agriProViewMapper.selectByExample(agriProViewExample);
        PageInfo<AgriProView> pageInfo = new PageInfo<>(page);
        pageInfo.setList(parks);
        return pageInfo;
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public boolean checkProCode(String proCode) {
        AgriProductExample example = new AgriProductExample();
        AgriProductExample.Criteria criteria = example.createCriteria();
        criteria.andProCodeEqualTo(proCode);
        long count = agriProductMapper.countByExample(example);
        if(count>0){
            return false;
        }else{
            return true;
        }
    }


    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public boolean checkProNmae(String proName) {
        AgriProductExample example = new AgriProductExample();
        AgriProductExample.Criteria criteria = example.createCriteria();
        criteria.andProNameEqualTo(proName);
        long count = agriProductMapper.countByExample(example);
        if(count>0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void saveAgriPro(AgriProduct agriProduct) {
        try {
            agriProductMapper.insert(agriProduct);
        }catch (Exception ex){
            LOGGER.error("保存农产品时出现错误", ex.fillInStackTrace());
            throw new ServiceException("保存农产品时出现错误", ex.getCause());
        }
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public boolean hasRef(String proId) {
        //1判断是否有园区与此产品关联
        FarmParkAgriProductExample farmParkAgriProductExample = new FarmParkAgriProductExample();
        FarmParkAgriProductExample.Criteria proExmapleCrieria = farmParkAgriProductExample.createCriteria();
        proExmapleCrieria.andProIdEqualTo(proId);
        long count = farmParkAgriProductMapper.countByExample(farmParkAgriProductExample);
        if(count>0){
            return true;
        }

        //2判断是否有生产环节与之相联
        ProducttionLinkExample producttionLinkExample = new ProducttionLinkExample();
        ProducttionLinkExample.Criteria producttionLinkExampleCriteria = producttionLinkExample.createCriteria();
        producttionLinkExampleCriteria.andProIdEqualTo(proId);
        count = producttionLinkMapper.countByExample(producttionLinkExample);
        if(count>0){
            return true;
        }

        //３判断是否有检测项目与之相关
        CheckSegmentExample checkSegmentExample = new CheckSegmentExample();
        CheckSegmentExample.Criteria checkSegmentExampleCriteria = checkSegmentExample.createCriteria();
        checkSegmentExampleCriteria.andProIdEqualTo(proId);
        count = checkSegmentMapper.countByExample(checkSegmentExample);
        if(count>0){
            return true;
        }

        return false;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void delAgriPro(String proId) {
        agriProductMapper.deleteByPrimaryKey(proId);
    }

    @Override
    public AgriProduct findAgriProductById(String proId) {
        return agriProductMapper.selectByPrimaryKey(proId);
    }

}
